package com.lb.jwitter.action;


import java.util.ArrayList; 
import java.util.List; 
import com.lb.jwitter.util.ApplicationContextProvider; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import java.util.Map; 
 import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblCTDTMonHocKem;
import com.lb.jwitter.service.TblCTDTMonHocKemService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblCTDTMonHocKemSearchAction{
    private int pageSize; 
    public void setPageSize(int pageSize) {
       this.pageSize = pageSize;
     }

    public int getPageSize() {
       return pageSize;
 }

    private int page; 
    public void setPage(int page) { this.page = page;   }

    public int getPage() {
       return page;
   }

    private int pages; 
    public void setPages(int pages) { this.pages = pages;   }

    public int getPages() {
       return pages;
     }

    private String dataString; 
    public void setDataString(String dataString) { this.dataString = dataString;    }

    public String getDataString() {
       return dataString;
    }

    private Message msg; 
    public Message getMsg() { return msg; }

    private List<TblCTDTMonHocKem> tblCTDTMonHocKems; 
    public List<TblCTDTMonHocKem>  getTblCTDTMonHocKems() {
        return tblCTDTMonHocKems; 
      } 
    
    private String maMonHoc;
    public String getMaMonHoc() {
		return maMonHoc;
	}

	public void setMaMonHoc(String maMonHoc) {
		this.maMonHoc = maMonHoc;
	}

	public String getMaMonHocKem() {
		return maMonHocKem;
	}

	public void setMaMonHocKem(String maMonHocKem) {
		this.maMonHocKem = maMonHocKem;
	}

	private String maMonHocKem;

    private TblCTDTMonHocKemService tblCTDTMonHocKemService; 

	private int preSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CTDT_MonHocKem", "xem")){ 
        return "noprivilege"; 
    } 
        tblCTDTMonHocKemService = (TblCTDTMonHocKemService) context.getBean("tbl_CTDT_MonHocKemClient"); 
        msg = new Message(); 
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        if(maMonHoc != null && !maMonHoc.equalsIgnoreCase("") ){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("monHocId.ma");
            con.setValue1(maMonHoc);
            con.setOperator("eq");
            cons.add(con);	
        } 
        
        if(maMonHocKem != null && !maMonHocKem.equalsIgnoreCase("") ){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("monHocKemId.ma");
            con.setValue1(maMonHocKem);
            con.setOperator("eq");
            cons.add(con);	
        } 
        
        preSearch(msg, cons); 
       if (msg.isPreMessage_b()) 
       { 
           if(pageSize == 0){ 
               pageSize = 20; 
           } 
            if(page == 0){ 
               page =1; 
           } 
           tblCTDTMonHocKems = tblCTDTMonHocKemService.findtblCTDTMonHocKemByCondition(cons,pageSize,page); 
           pages = tblCTDTMonHocKemService.counttblCTDTMonHocKemByCondition(cons)/pageSize + 1; 
           postSearch(msg, cons); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
      dataString = "";
      dataString += "&maMonHoc=" + maMonHoc; 
      dataString += "&maMonHocKem=" + maMonHocKem; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Seach tbl_CTDT_MonHocKem Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
